package x10.event;

import java.io.Serializable;

import x10.enums.Security;

/**
 * X10 Security Sensor class - represents a security sensor object
 * used for DS10A door/window sensor, MS10A motion sensor, etc.
 *
 * @author Denny Sheirer
 * @date 20101226
 */
public class X10SecuritySensor extends X10Event implements Serializable {
    static final long serialVersionUID = 1L;
    private String address;
    private String source;
    private long instantiatedMillis;

    private Security contact;  //Magnetic reed switch contacts (opn or closed)
    private Security delay;    //Min or max delay
    private Security battery;  //Battery good or bad
    private Security motion;   //Motion or no motion

    /**
     * Constructor
     * @param securityCode - int value of 2 byte security code - cast to a string 
     */
    public X10SecuritySensor(String securityCode){
        this.address = securityCode;
        this.instantiatedMillis = System.currentTimeMillis();
    }
    
    /**
     * Constructor
     * @param securityCode
     * @param contact
     * @param motion
     * @param delay
     * @param batteryLow
     */
    public X10SecuritySensor(String securityCode, Security contact, Security motion, Security delay, Security batteryLow, String source){
	this(securityCode);
        this.contact = contact;
        this.motion = motion;
        this.delay = delay;
        this.battery = batteryLow;
        this.source = source;
    }

    public Security getMotionStatus() {
        return motion;
    }

    public void setMotionStatus(Security motion) {
        this.motion = motion;
    }

    public Security getContactStatus() {
        return contact;
    }

    public void setContactStatus(Security contact) {
        this.contact = contact;
    }

    public Security getBatteryStatus() {
        return battery;
    }

    public void setBatteryStatus(Security battery) {
        this.battery = battery;
    }

    public Security getDelayStatus() {
        return delay;
    }

    public void setDelayStatus(Security delay) {
        this.delay = delay;
    }

    /**
     * @return the source
     */
    public String getSource() {
        return source;
    }

    /**
     * @param source the source to set
     */
    public void setSource(String source) {
        this.source = source;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the instantiatedMillis
     */
    public long getInstantiatedMillis() {
        return instantiatedMillis;
    }

    /**
     * @param instantiatedMillis the instantiatedMillis to set
     */
    public void setInstantiatedMillis(long instantiatedMillis) {
        this.instantiatedMillis = instantiatedMillis;
    }

    public String toString(){
        StringBuilder sb = new StringBuilder();
        sb.append("X10 Security Sensor ");
        sb.append("ID:");
        sb.append(address);
        sb.append("  ");
        sb.append("Contact:");
        sb.append(contact);
        sb.append("  ");
        sb.append("Motion:");
        sb.append(motion);
        sb.append("  ");
        sb.append("Delay:");
        sb.append(delay);
        sb.append("  ");
        sb.append("Battery:");
        sb.append(battery);
        sb.append("  ");
        sb.append("Source:");
        sb.append(source);

        return sb.toString();
    }

    public boolean equals(X10Event device){
        boolean isEqual = true;

        if(device instanceof X10SecuritySensor){
            X10SecuritySensor other = (X10SecuritySensor)device;
            if(other!=null){
                if(!this.getAddress().contentEquals(other.getAddress())){
                    isEqual = false;
                }
                if(!this.getSource().contentEquals(other.getSource())){
                    isEqual = false;
                }
                if(this.contact != other.getContactStatus()){
                    isEqual = false;
                }
                if(this.delay != other.getDelayStatus()){
                    isEqual = false;
                }
                if(this.motion != other.getMotionStatus()){
                    isEqual = false;
                }
                if(this.battery != other.getBatteryStatus()){
                    isEqual = false;
                }

                //If both objects are within 1 second of each other, call them equal
                long low = this.getInstantiatedMillis() - 1000; //1 second earlier
                long high = this.getInstantiatedMillis() + 1000; //1 second later
                long otherMillis = other.getInstantiatedMillis();

                if(!((otherMillis >= low) && (otherMillis <= high))){
                    isEqual = false;
                }
            }
        } else {
            isEqual = false;
        }

        return isEqual;
    }

}
